﻿#include "modelset.h"

modelSet::modelSet()
{

}

void modelSet::setCourseTitle(QStandardItemModel *model)
{
    model->clear();
    model->setHorizontalHeaderItem(0,new QStandardItem("选课编号"));
    model->setHorizontalHeaderItem(1,new QStandardItem("课程代码"));
    model->setHorizontalHeaderItem(2,new QStandardItem("课程名称"));
    model->setHorizontalHeaderItem(3,new QStandardItem("教学班号"));
    model->setHorizontalHeaderItem(4,new QStandardItem("学分"));
    model->setHorizontalHeaderItem(5,new QStandardItem("性质"));
    model->setHorizontalHeaderItem(6,new QStandardItem("开课学院"));
    model->setHorizontalHeaderItem(7,new QStandardItem("教师"));
    model->setHorizontalHeaderItem(8,new QStandardItem("职称"));
    model->setHorizontalHeaderItem(9,new QStandardItem("上课时间"));
    model->setHorizontalHeaderItem(10,new QStandardItem("校区"));
    model->setHorizontalHeaderItem(11,new QStandardItem("最大容量"));
    model->setHorizontalHeaderItem(12,new QStandardItem("已选人数"));
}

void modelSet::setStudentTitle(QStandardItemModel *model)
{
    model->setHorizontalHeaderItem(0,new QStandardItem("姓名"));
    model->setHorizontalHeaderItem(1,new QStandardItem("学号"));
    model->setHorizontalHeaderItem(2,new QStandardItem("年级"));
    model->setHorizontalHeaderItem(3,new QStandardItem("班级"));
    model->setHorizontalHeaderItem(4,new QStandardItem("学院"));
    model->setHorizontalHeaderItem(5,new QStandardItem("专业"));
    model->setHorizontalHeaderItem(6,new QStandardItem("所选课程"));
}

void modelSet::setTeacherTitle(QStandardItemModel *model)
{
    model->setHorizontalHeaderItem(0,new QStandardItem("姓名"));
    model->setHorizontalHeaderItem(1,new QStandardItem("工号"));
    model->setHorizontalHeaderItem(2,new QStandardItem("学院"));
    model->setHorizontalHeaderItem(3,new QStandardItem("职称"));
    model->setHorizontalHeaderItem(4,new QStandardItem("所授课程"));
}

void modelSet::setStudentItem(QStandardItemModel *model,const QStringList& _linesplit,const int& currow)
{
    model->setItem(currow,0,new QStandardItem(_linesplit.at(0)));
    model->setItem(currow,1,new QStandardItem(_linesplit.at(1)));
    model->setItem(currow,2,new QStandardItem(_linesplit.at(2)));
    model->setItem(currow,3,new QStandardItem(_linesplit.at(3)));
    model->setItem(currow,4,new QStandardItem(_linesplit.at(4)));
    model->setItem(currow,5,new QStandardItem(_linesplit.at(5)));
    for(int i=6;i<_linesplit.length();i++)
        model->setItem(currow,i,new QStandardItem(_linesplit.at(i)));
}

void modelSet::setCourseItem(QStandardItemModel *model,const QStringList& _linesplit,const int& currow)
{
    model->setItem(currow,0,new QStandardItem(_linesplit.at(0)));
    model->setItem(currow,1,new QStandardItem(_linesplit.at(1)));
    model->setItem(currow,2,new QStandardItem(_linesplit.at(2)));
    model->setItem(currow,3,new QStandardItem(_linesplit.at(3)));
    model->setItem(currow,4,new QStandardItem(_linesplit.at(4)));
    model->setItem(currow,5,new QStandardItem(_linesplit.at(5)));
    model->setItem(currow,6,new QStandardItem(_linesplit.at(6)));
    model->setItem(currow,7,new QStandardItem(_linesplit.at(7)));
    model->setItem(currow,8,new QStandardItem(_linesplit.at(8)));
    model->setItem(currow,9,new QStandardItem(_linesplit.at(9)));
    model->setItem(currow,10,new QStandardItem(_linesplit.at(10)));
    model->setItem(currow,11,new QStandardItem(_linesplit.at(11)));
    model->setItem(currow,12,new QStandardItem(_linesplit.at(12)));
    model->setItem(currow,13,new QStandardItem(_linesplit.at(13)));
}

void modelSet::setTeacherItem(QStandardItemModel *model,const QStringList& _linesplit,const int& curRow)
{
    model->setItem(curRow,0,new QStandardItem(_linesplit.at(0)));
    model->setItem(curRow,1,new QStandardItem(_linesplit.at(1)));
    model->setItem(curRow,2,new QStandardItem(_linesplit.at(2)));
    model->setItem(curRow,3,new QStandardItem(_linesplit.at(3)));
    for(int i=4;i<_linesplit.length();i++)
    {
        model->setItem(curRow,i,new QStandardItem(_linesplit.at(i)));
    }
}

void modelSet::Swap(int p1, int p2, QStandardItemModel *model)
{
    for(int i = 0; i < model->columnCount();i++)
    {
        QStandardItem* t1 = NULL;
        QStandardItem* t2 = NULL;
        if(model->item(p1,i)!=NULL)
            t1 = model->item(p1,i)->clone();
        if(model->item(p2,i)!=NULL)
            t2 = model->item(p2,i)->clone();
        model->setItem(p1,i,t2);
        model->setItem(p2,i,t1);
    }
}

int modelSet::partition(int p, int q, int column,QStandardItemModel *model)
{
    QString pivot = model->item(q, column)->text();
    int i = p - 1;
    for(int j = p; j <= q - 1;j++) {
        if(model->item(j,column)->text()<=pivot)
        {
            i++;
            Swap(j,i,model);
        }
        else if(!(model->item(j,column)->text()>=pivot))
        {
            i++;
            Swap(j,i,model);
        }
    }
    Swap(q,i+1,model);
    return i+1;
}
//以下分别为升降序
void modelSet::quickSort(int p, int q, int column,QStandardItemModel *model)
{
    if(p < q)
    {
        int r = partition(p,q,column,model);
        quickSort(p,r-1,column,model);
        quickSort(r+1,q,column,model);
    }
}

void modelSet::quickSort_1(int p, int q, int column, QStandardItemModel *model)
{
    quickSort(p,q,column,model);
    for(int i=0;i<model->rowCount()/2;i++)
    {
        Swap(i,model->rowCount()-1-i,model);
    }
}
